package com.nicefact.mailmonitor.entity;
import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.listener.StoreCallback;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(detachable="true")
public class AppUser  implements Serializable,StoreCallback {
	
	private static final long serialVersionUID = 13L;
	
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@PrimaryKey
	private Key key;
	
	@Persistent
	private String email;
	
	@Persistent
	private String contextioId;
	
	@Persistent
	private String firstName;
	
	@Persistent
	private String lastName;
	
	@Persistent
	private String createdBy;
	
	@Persistent
	private Date createdDate = new Date();
	
	
	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	@Persistent
	private String domainName; 
	
	@Persistent
	private String oauthtoken;
	
	@Persistent
	private String oauthtokensecret;

	
	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	public String getContextioId() {
		return contextioId;
	}

	public void setContextioId(String contextioId) {
		this.contextioId = contextioId;
	}

	@Persistent
	private String accessTokenKey;
	
	@Persistent
	private String accessTokenSecret;
	

	public String getAccessTokenKey() {
		return accessTokenKey;
	}

	public void setAccessTokenKey(String accessTokenKey) {
		this.accessTokenKey = accessTokenKey;
	}

	public String getAccessTokenSecret() {
		return accessTokenSecret;
	}

	public void setAccessTokenSecret(String accessTokenSecret) {
		this.accessTokenSecret = accessTokenSecret;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getDomainName() {
		return domainName;
	}

	public void setDomainName(String domainName) {
		this.domainName = domainName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getOauthtoken() {
		return oauthtoken;
	}

	public void setOauthtoken(String oauthtoken) {
		this.oauthtoken = oauthtoken;
	}

	public String getOauthtokensecret() {
		return oauthtokensecret;
	}

	public void setOauthtokensecret(String oauthtokensecret) {
		this.oauthtokensecret = oauthtokensecret;
	}

	
	
	public AppUser() {
		super();
		// TODO Auto-generated constructor stub
	}

	public AppUser(String email, String firstName, String lastName, String adminEmail) {
		super();
		this.email = email;
		this.firstName = firstName;
		this.lastName = lastName;
		this.createdBy = adminEmail;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result
				+ ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result
				+ ((lastName == null) ? 0 : lastName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AppUser other = (AppUser) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "AppUser [key=" + key + ", email=" + email + ", domaiName="
				+ domainName + ", oauthtoken=" + oauthtoken
				+ ", oauthtokensecret=" + oauthtokensecret + "]";
	}

	@Override
	public void jdoPreStore() {
		if(!StringUtils.isBlank(firstName)){
			firstName = firstName.toUpperCase();			
		}
		if(!StringUtils.isBlank(lastName)){			
			lastName = lastName.toUpperCase();
		}
		
	}
	
	
	

}
